﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace EasySL.Controls
{
    public class ExFilterMenu : ContentControl
    {
        public Action<ExFilterAction, string> OnClickItem { get; set; }
        public string BindProperty { get; set; }
        
        public ExFilterMenu()
            : base()
        {
            this.DefaultStyleKey = typeof(ExFilterMenu);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.menuContent = this.GetTemplateChild("menuContent") as Panel;
            this.menu = this.GetTemplateChild("menu") as Panel;
        }

        public void AddMenuItem(string item, ExFilterAction action)
        {
            AddMenuItem(item, action, null);
        }

        public void AddMenuItem(string item, ExFilterAction action, Action<ExFilterAction> onClick)
        {
            if (menu == null)
                return;
            
            Button menuItem = new Button();
            menuItem.Content = item;

            menuItem.Style = (Style)this.menuContent.Resources["menuItemStyle"];
            menu.Children.Add(menuItem);

            menuItem.Click += (o, e) =>
            {
                if (onClick != null)
                    onClick(action);

                if (OnClickItem != null)
                    OnClickItem(action, BindProperty);
            };
        }

        public void AddSplitItem()
        {
            if (menu == null)
                return;
            
            Border border = new Border();
            border.BorderBrush = new SolidColorBrush(Color.FromArgb(0xFF, 0xC0, 0xC0, 0xC0));
            border.BorderThickness = new Thickness(0, 0, 0, 1);
            border.Margin = new Thickness(5, 2, 0, 2);
            border.Height = 5;

            menu.Children.Add(border);
        }

        public void ClearItems()
        {
            if (menu != null)
            {
                menu.Children.Clear();
            }
        }

        private Panel menuContent;
        private Panel menu;

    }
}
